﻿using LCL.Network.Enthernet;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace LCL.PushNetServer
{
    public partial class FrmPushNetServer : Form
    {
        private Timer timerOneSecond;
        private NetPushServer pushServer;
        public FrmPushNetServer()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            panel2.Enabled = false;
            timerOneSecond = new Timer();
            timerOneSecond.Interval = 1000;
            timerOneSecond.Tick += TimerOneSecond_Tick;
            textBox3.Text = Guid.Empty.ToString();
        }
        private void TimerOneSecond_Tick(object sender, EventArgs e)
        {
            label2.Text = pushServer.OnlineCount.ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                // 启动服务
                pushServer = new NetPushServer();
                pushServer.Token = new Guid(textBox3.Text);                                    // 支持令牌
                pushServer.LogNet = new LogNet.LogNetSingle("NetPushServer.txt");       // 支持日志
                pushServer.PushCacheAfterConnect = checkBox1.Checked;
                pushServer.ServerStart(int.Parse(textBox2.Text));
                button1.Enabled = false;
                panel2.Enabled = true;
                timerOneSecond.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            // 普通的数据推送
            pushServer.PushString(textBox5.Text, textBox4.Text);
        }
        private void button7_Click(object sender, EventArgs e)
        {
            try
            {
                //远程推送
                Result create = pushServer.CreatePushRemote(textBox9.Text, int.Parse(textBox10.Text), textBox15.Text);
                if (create.Success)
                {
                    MessageBox.Show("创建成功！");
                }
                else
                {
                    MessageBox.Show("创建失败！" + create.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        // ========================================================================================================
        //随机数推送
        private Timer timer随机数;
        private int min = 0;
        private int max = 1000;
        private Random random = new Random();
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                // 定时推送随机数
                timer随机数 = new Timer();
                timer随机数.Interval = int.Parse(textBox6.Text);
                min = int.Parse(textBox7.Text);
                max = int.Parse(textBox8.Text);

                timer随机数.Tick += Timer随机数_Tick;
                timer随机数.Start();
                button3.Enabled = false;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void Timer随机数_Tick(object sender, EventArgs e)
        {
            pushServer.PushString(textBox1.Text, random.Next(min, max).ToString());
        }
        // 时间推送
        private Timer timer时间;
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                // 时间推送
                timer时间 = new Timer();
                timer时间.Interval = int.Parse(textBox11.Text);
                timer时间.Tick += Timer时间_Tick;
                timer时间.Start();
                button4.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void Timer时间_Tick(object sender, EventArgs e)
        {
            pushServer.PushString(textBox12.Text, DateTime.Now.ToString());
        }
        // 推送json消息
        private Timer timerJson;
        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                timerJson = new Timer();
                timerJson.Interval = int.Parse(textBox13.Text);
                timerJson.Tick += TimerJson_Tick;
                timerJson.Start();
                button5.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void TimerJson_Tick(object sender, EventArgs e)
        {
            JObject json = new JObject();
            json.Add("value1", new JValue(random.Next(1000, 9999)));
            json.Add("value2", new JValue(Math.Round(random.NextDouble(), 6) * 1000));
            json.Add("value3", new JValue(Guid.NewGuid().ToString()));
            json.Add("value4", new JValue(DateTime.Now));

            pushServer.PushString(textBox14.Text, json.ToString());
        }
        //Xml推送
        private Timer timerXml;
        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                timerXml = new Timer();
                timerXml.Interval = int.Parse(textBox17.Text);
                timerXml.Tick += TimerXml_Tick; ;
                timerXml.Start();
                button6.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void TimerXml_Tick(object sender, EventArgs e)
        {
            XElement element = new XElement("Data");
            element.SetElementValue("value1", random.Next(1000, 9999));
            element.SetElementValue("value2", (Math.Round(random.NextDouble(), 6) * 1000).ToString());
            element.SetElementValue("value3", Guid.NewGuid().ToString());
            element.SetElementValue("value4", DateTime.Now.ToString("O"));

            pushServer.PushString(textBox18.Text, element.ToString());
        }

        private void myIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                myMenu.Show();
            }
            if (e.Button == MouseButtons.Left)
            {
                this.Visible = true;
                this.WindowState = FormWindowState.Normal;
            }     
        }

        private void myIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //判断是否已经最小化于托盘 
            if (WindowState == FormWindowState.Minimized)
            {
                //还原窗体显示 
                WindowState = FormWindowState.Normal;
                //激活窗体并给予它焦点 
                this.Activate();
                //任务栏区显示图标 
                this.ShowInTaskbar = true;
                //托盘区图标隐藏 
                myIcon.Visible = false;
            }
        }

        private void 显示窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.Visible == false) this.Visible = true;//假如当前窗口没显示则显示当前窗口 
        }

        private void 隐藏窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.Visible == true) this.Visible = false;//假如当前窗口为显示的则隐藏窗口 
        }

        private void 退出程序ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();//关闭当前对象(即窗体) 
            Application.Exit();//通过Application类的静态方法Exit()退出应用程序 
        }

        private void FrmPushNetServer_SizeChanged(object sender, EventArgs e)
        {
            //判断是否选择的是最小化按钮 
            if (WindowState == FormWindowState.Minimized)
            {
                //隐藏任务栏区图标 
                this.ShowInTaskbar = false;
                //图标显示在托盘区 
                myIcon.Visible = true;
            }
        }

        private void FrmPushNetServer_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)//当用户点击窗体右上角X按钮或(Alt + F4)时 发生      
            {
                e.Cancel = true;
                this.ShowInTaskbar = false;
                this.myIcon.Icon = this.Icon;
                this.Hide();
            }    
        }
    }
}
